package com.example.composableexample.pager

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.MaterialTheme
import androidx.compose.material.ScrollableTabRow
import androidx.compose.material.Tab
import androidx.compose.material.TabRowDefaults
import androidx.compose.material.TopAppBar
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.example.composableexample.ui.theme.ComposableExampleTheme
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.PagerScope
import com.google.accompanist.pager.PagerState
import com.google.accompanist.pager.pagerTabIndicatorOffset
import com.google.accompanist.pager.rememberPagerState
import kotlinx.coroutines.launch

@OptIn(ExperimentalPagerApi::class)
@Composable
fun HomeTab(
    modifier: Modifier = Modifier,
    pagerState: PagerState,
    count: Int,
    label: @Composable (Int, Boolean) -> Unit
) {
    ScrollableTabRow(
        edgePadding = 18.dp,
        divider = {},
        modifier = modifier,
        indicator = {
            TabRowDefaults.Indicator(
                modifier = Modifier.pagerTabIndicatorOffset(pagerState, it)
            )
        },
        selectedTabIndex = pagerState.currentPage
    ) {
        repeat(count) {
            label(it, pagerState.currentPage == it)
        }
    }
}

@OptIn(ExperimentalPagerApi::class)
@Composable
fun HomePagerLayout(
    modifier: Modifier = Modifier,
    pagerState: PagerState,
    count: Int,
    pager: @Composable PagerScope.(Int) -> Unit
) {
    HorizontalPager(
        count = count,
        state = pagerState,
        modifier = modifier,
        content = pager
    )
}


@Composable
fun BehaviorToolbarScaffold(
    toolbarSize: Dp = 56.dp,
    toolbar: @Composable () -> Unit,
    content: @Composable (NestedScrollConnection) -> Unit
) {
    val offset = remember { mutableStateOf(0f) }
    val nestedScrollConnection = object : NestedScrollConnection {
        override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
            var offsetY = (offset.value) + available.y
            var connection = available.y
            if (offsetY >= 0f) {
                offsetY = 0f
                connection = 0 - (offset.value)
            }
            if (offsetY <= -(toolbarSize.value)) {
                offsetY = -(toolbarSize.value)
                connection = -(toolbarSize.value) - (offset.value)
            }
            offset.value = offsetY
            return Offset(0f, connection)
        }
    }
    Box {
        Column {
            Spacer(modifier = Modifier.height((toolbarSize.value - (-offset.value)).dp))
            content(nestedScrollConnection)
        }
        Box(modifier = Modifier
            .fillMaxWidth()
            .height(toolbarSize)
            .graphicsLayer {
                translationY = (offset.value)
            }
        ) {
//            toolbar()
        }
    }
}

@OptIn(ExperimentalPagerApi::class)
@Composable
fun TestPreview() {
    val pagerState = rememberPagerState()
    val scope = rememberCoroutineScope()

    Column {
        BehaviorToolbarScaffold(
            toolbar = {
                TopAppBar(
                    elevation = 0.dp,
                    modifier = Modifier
                        .fillMaxWidth(),
                    title = {
                        Text(text = "TopAppbar标题")
                    }
                )
            }
        ) { nestedScrollConnection ->
            HomeTab(
//                modifier = Modifier.height(56.dp),
                pagerState = pagerState,
                count = 2
            ) { index, selected ->
                Tab(
                    selected = selected,
                    onClick = {
                        scope.launch {
                            pagerState.animateScrollToPage(index)
                        }
                    }
                ) {
                    Text(text = "目录$index")
                }
            }

            HomePagerLayout(
                modifier = Modifier.fillMaxSize(),
                pagerState = pagerState,
                count = 2
            ) {
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                ) {
                    when (it) {
                        0 -> TestPage(nestedScrollConnection = nestedScrollConnection)
                        1 -> TestPage(nestedScrollConnection = nestedScrollConnection)
                    }
                }
            }
        }
    }
}

@Composable
fun TestPage(nestedScrollConnection: NestedScrollConnection) {
    LazyColumn(
        modifier = Modifier
            .fillMaxWidth()
            .nestedScroll(nestedScrollConnection)
    ) {
        items(1000) {
            Text(text = "item$it")
        }
    }
}

@Preview
@Composable
fun TabPreview() {
    ComposableExampleTheme {
        TestPreview()
    }
}













